package com.lgmshare.k3.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lgmshare.k3.R;


/**
 * @author: lim
 * @ClassName: LoadingDialog
 * @description: 加载等待框
 * @email: lgmshare@gmail.com
 * @datetime 2014-6-3  上午11:03:42
 */
public class LoadingDialog extends Dialog {

    private TextView dialog_tips_msg;
    private ProgressBar dialog_progressbar;

    public LoadingDialog(Context context) {
        this(context, R.style.CommonDialog);
    }

    public LoadingDialog(Context context, int theme) {
        super(context, theme);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_loading);
        dialog_tips_msg = findViewById(R.id.dialog_tips_msg);
        dialog_progressbar = findViewById(R.id.dialog_progressbar);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void setMessage(int resId) {
        setMessage(getContext().getResources().getString(resId));
    }

    public void setMessage(CharSequence message) {
        if (dialog_tips_msg != null) {
            dialog_tips_msg.setVisibility(View.VISIBLE);
            dialog_tips_msg.setText(message);
        }
    }

    public static class Builder {

        private Context mContext;
        private int mThemeId;
        private CharSequence mMessage;
        private boolean mCancelable;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder(Context context, int themeResId) {
            mContext = context;
            mThemeId = themeResId;
        }

        public Context getContext() {
            return mContext;
        }


        public LoadingDialog.Builder setMessage(int messageId) {
            mMessage = getContext().getResources().getString(messageId);
            return this;
        }

        public LoadingDialog.Builder setMessage(CharSequence message) {
            mMessage = message;
            return this;
        }

        public LoadingDialog.Builder setCancelable(boolean cancelable) {
            mCancelable = cancelable;
            return this;
        }

        public LoadingDialog create() {
            // Context has already been wrapped with the appropriate theme.
            final LoadingDialog dialog = new LoadingDialog(mContext, mThemeId);
            dialog.setCancelable(mCancelable);
            dialog.setMessage(mMessage);
            return dialog;
        }

        public LoadingDialog show() {
            // show
            final LoadingDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }
}